package com.yuou.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuou.common.base.CoreServiceImpl;
import com.yuou.common.utils.BuildTree;
import com.yuou.common.utils.Tree;
import com.yuou.mapper.DeptMapper;
import com.yuou.mapper.MenuMapper;
import com.yuou.model.Dept;
import java.util.Map;
import com.yuou.service.DeptService;
@Service
public class DeptServiceImpl extends CoreServiceImpl<DeptMapper, Dept> implements  DeptService {
	@Autowired
	private DeptMapper deptMapper;
	@Override
    public Tree<Dept> getTree() {
        List<Tree<Dept>> trees = new ArrayList<Tree<Dept>>();
        List<Dept> sysDepts = deptMapper.selectAll(null);
        for (Dept sysDept : sysDepts) {
            Tree<Dept> tree = new Tree<Dept>();
            tree.setId(sysDept.getId().toString());
            tree.setParentId(sysDept.getParentId().toString());
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<Dept> t = BuildTree.build(trees);
        return t;
    }

	@Override
    public boolean checkDeptHasUser(int deptId) {
        // 查询部门以及此部门的下级部门
        int result = baseMapper.selectCountByid(deptId);
        return result == 0;
    }

	@Override
	public List<Dept> selectAll(Map<String, Object> map) {
		return deptMapper.selectAll(map);
	}

	@Override
	public Dept selectById(Integer id) {
		return deptMapper.selectById(id);
	}

	@Override
	public String selectNameByid(Integer parentId) {
		return deptMapper.selectNameByid(parentId);
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return deptMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int selectCountByid(Integer id) {
		return deptMapper.selectCountByid(id);
	}

	@Override
	public int updateByPrimaryKey(Dept dept) {
		return deptMapper.updateByPrimaryKey(dept);
	}

	@Override
	public int insertDept(Dept dept) {
		return deptMapper.insertDept(dept);
	}
}
